Conditions | 1 |
Paths | 192 |
Total Lines | 73 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | exports.parse = exports.decode = decode |
||
69 | function decode (str) { |
||
70 | var out = {} |
||
71 | var p = out |
||
72 | var section = null |
||
73 | // section |key = value |
||
74 | var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i |
||
75 | var lines = str.split(/[\r\n]+/g) |
||
76 | |||
77 | lines.forEach(function (line, _, __) { |
||
78 | if (!line || line.match(/^\s*[;#]/)) return |
||
79 | var match = line.match(re) |
||
80 | if (!match) return |
||
81 | if (match[1] !== undefined) { |
||
82 | section = unsafe(match[1]) |
||
83 | p = out[section] = out[section] || {} |
||
84 | return |
||
85 | } |
||
86 | var key = unsafe(match[2]) |
||
87 | var value = match[3] ? unsafe(match[4]) : true |
||
88 | switch (value) { |
||
89 | case 'true': |
||
90 | case 'false': |
||
91 | case 'null': value = JSON.parse(value) |
||
92 | } |
||
93 | |||
94 | // Convert keys with '[]' suffix to an array |
||
95 | if (key.length > 2 && key.slice(-2) === '[]') { |
||
96 | key = key.substring(0, key.length - 2) |
||
97 | if (!p[key]) { |
||
98 | p[key] = [] |
||
99 | } else if (!Array.isArray(p[key])) { |
||
100 | p[key] = [p[key]] |
||
101 | } |
||
102 | } |
||
103 | |||
104 | // safeguard against resetting a previously defined |
||
105 | // array by accidentally forgetting the brackets |
||
106 | if (Array.isArray(p[key])) { |
||
107 | p[key].push(value) |
||
108 | } else { |
||
109 | p[key] = value |
||
110 | } |
||
111 | }) |
||
112 | |||
113 | // {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}} |
||
114 | // use a filter to return the keys that have to be deleted. |
||
115 | Object.keys(out).filter(function (k, _, __) { |
||
116 | if (!out[k] || |
||
117 | typeof out[k] !== 'object' || |
||
118 | Array.isArray(out[k])) { |
||
119 | return false |
||
120 | } |
||
121 | // see if the parent section is also an object. |
||
122 | // if so, add it to that, and mark this one for deletion |
||
123 | var parts = dotSplit(k) |
||
124 | var p = out |
||
125 | var l = parts.pop() |
||
126 | var nl = l.replace(/\\\./g, '.') |
||
127 | parts.forEach(function (part, _, __) { |
||
128 | if (!p[part] || typeof p[part] !== 'object') p[part] = {} |
||
129 | p = p[part] |
||
130 | }) |
||
131 | if (p === out && nl === l) { |
||
132 | return false |
||
133 | } |
||
134 | p[nl] = out[k] |
||
135 | return true |
||
136 | }).forEach(function (del, _, __) { |
||
137 | delete out[del] |
||
138 | }) |
||
139 | |||
140 | return out |
||
141 | } |
||
142 | |||
195 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.